package com.example.administrator.lrucachedemo

import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private val imageDownLoader=ImageDownLoader()
    private val handler=object:Handler(){
        override fun handleMessage(msg: Message?) {
            Log.d(TAG,"handler handlerMessage:"+Thread.currentThread().name)
            if(msg?.what==110){
                imageView.setImageBitmap(msg?.obj as Bitmap?)
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showBitmap("https://www.baidu.com/img/bd_logo1.png")
    }
    private fun showBitmap(key:String){
        val bitmap=imageDownLoader.getBitmapFromMemCache(key)
        if(bitmap==null){
            BitmapThread(key,imageDownLoader,handler).start()
        }else{
            imageView.setImageBitmap(bitmap)
        }
    }
    companion object {
        private val TAG="测试Activity"
    }
}
